<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库存组织" prop="orgguid" >
        <el-select size="mini" v-model="queryParams.orgguid" placeholder="请选择库存组织" filterable clearable @change="getSRoomByOrgId" >
          <el-option
            v-for="item in orgOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库房" prop="srguid">
        <el-select v-model="queryParams.srguid" placeholder="请选择库房" filterable clearable size="mini">
          <el-option
            v-for="item in roomOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="领料单号" prop="plcode">
        <el-input
          v-model="queryParams.plcode"
          placeholder="请输入领料单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库类型" prop="intype">
        <el-select v-model="queryParams.intype" placeholder="请选择出库类型" clearable size="mini">
          <el-option label="主料" value="1" />
          <el-option label="辅料" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:producereport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bill:producereport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:producereport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:producereport:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="producereportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库存组织" align="center" prop="orgname" />
      <el-table-column label="库房" align="center" prop="srname" width="150" />
      <el-table-column label="领料单编号" align="center" prop="plcode" />
      <el-table-column label="出库数量" align="center" prop="outweight" />
      <el-table-column label="入库数量" align="center" prop="inweight" />
      <el-table-column label="退料数量" align="center" prop="backweight" />
      <el-table-column label="报废数量" align="center" prop="inweight" />
<!--      <el-table-column label="领料单时间" align="center" prop="pldate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.pldate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="部门名称" align="center" prop="departname" />-->
      <el-table-column label="业务时间" align="center" prop="orderdate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="intype">
      <template slot-scope="scope">
          <span v-if="scope.row.intype== 1">主料</span>
          <span v-if="scope.row.intype== 2">辅料</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['bill:producereportitem:list']"
          >明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body>
      <el-table v-loading="loading" :data="producereportitemList" @selection-change="handleSelectionChange">
<!--      <el-table-column label="部门名称" align="center" prop="departname" />-->
      <el-table-column label="库房名称" align="center" prop="srname" />
      <!-- <el-table-column label="出库间名称" align="center" prop="shrname" /> -->
      <el-table-column label="出库产品" align="center" prop="outproduct" >
        <template slot-scope="scope">
          <span>{{ scope.row.outproduct + ' ' + scope.row.outspecname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库数量" align="center" prop="outweight" />
<!--      <el-table-column label="单位" align="center" prop="outstockunit" />-->
      <el-table-column label="入库产品" align="center" prop="inproduct" >
        <template slot-scope="scope">
        <span>{{ scope.row.inproduct + ' ' + scope.row.inspecname }}</span>
      </template>
      </el-table-column>
      <!-- <el-table-column label="入库规格类型" align="center" prop="insptype" /> -->
      <el-table-column label="入库数量" align="center" prop="inweight" />
      <el-table-column label="出成率" align="center" prop="inweight" />
      <el-table-column label="单位" align="center" prop="instockunit" />
      <!-- <el-table-column label="损耗" align="center" prop="lossweight" /> -->
<!--      <el-table-column label="成本对象" align="center" prop="cospname" v-if="cosShow"/>-->
    </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listProducereport, getProducereport, delProducereport, addProducereport, updateProducereport, exportProducereport, listProducereportitem } from "@/api/bill/producereport";
import { findOrganizationList, getSRoomByOrgId } from "@/api/public";

export default {
  name: "Producereport",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产报表格数据
      producereportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plcode: null,
        intype: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      producereportitemList: [],
      cosShow: true,
      orgOptions: [],
      roomOptions: [],
    };
  },
  created() {
    this.getList();
    this.findOrganizationList();
  },
  methods: {
    //查询组织
    findOrganizationList(){
      findOrganizationList().then(response => {
        this.orgOptions = response.data;
      });
    },
    //查询库房
    getSRoomByOrgId(orgId){
      getSRoomByOrgId({guid: orgId}).then(response => {
        this.roomOptions = response.data;
      });
    },
    /** 查询生产报列表 */
    getList() {
      this.loading = true;
      listProducereport(this.queryParams).then(response => {
        this.producereportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plcode: null,
        plstatus: 0,
        pldate: null,
        orgname: null,
        orgguid: null,
        inweight: null,
        outweight: null,
        backweight: null,
        starttime: null,
        endtime: null,
        creattime: null,
        departid: null,
        departname: null,
        srguid: null,
        srname: null,
        planguid: null,
        orderdate: null,
        operatorid: null,
        intype: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleDetail(row){
      listProducereportitem({producereportid:row.id}).then(response => {
        this.producereportitemList = response.rows;
        this.open = true;
      this.title = "生产报表明细";
      if(row.intype == 1){
          this.cosShow = false;
      }else{
         this.cosShow = true;
      }
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProducereport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProducereport(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProducereport(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProducereport(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有生产报数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          queryParams.pageSize=10000
          return exportProducereport(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
